<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/testdriver.js"></script>
<script src="/resources/testdriver-vendor.js"></script>
<script>
promise_test(async t => {
  t.add_cleanup(async () => {
    try {
      await document.exitFullscreen();
    } catch (e) {}
  });
  await test_driver.bless("request full screen", () => {
    return document.documentElement.requestFullscreen();
  });
  const type = screen.orientation.type;
  screen.orientation.onchange = t.unreached_func("change event should not be fired");
  await screen.orientation.lock(type);
  assert_equals(screen.orientation.type, type);
}, "Test that orientationchange event is not fired when the orientation does not change.");

promise_test(async t => {
  t.add_cleanup(async () => {
    try {
      await document.exitFullscreen();
    } catch (e) {}
    screen.orientation.unlock();
  });
  await test_driver.bless("request full screen", () => {
    return document.documentElement.requestFullscreen();
  });
  let orientations = [
    'portrait-primary',
    'portrait-secondary',
    'landscape-primary',
    'landscape-secondary'
  ];
  if (screen.orientation.type.includes('portrait')) {
    orientations = orientations.reverse();
  }
  const orientationWatcher = new EventWatcher(t, screen.orientation, 'change');

  for (const orientation of orientations) {
    await screen.orientation.lock(orientation);
    await orientationWatcher.wait_for('change');
    assert_equals(screen.orientation.type, orientation);
  }
}, "Test that orientationchange event is fired when the orientation changes.");
</script>
